package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.api.exception.IFastApiException;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.type.ApiEnumError;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.TrackDrawDO;
import com.jinzhi.jzweb.service.TrackDrawService;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 
 * <pre>
 * 赛道抽签表（存储抽签信息）
 * </pre>
 * <small> 2023-10-16 16:36:18 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/trackDrawGuangDong")
public class TrackDrawGuangDongController extends BaseController {

	@Autowired
	private TrackDrawService trackDrawService;
	
	@GetMapping()
	String TrackDraw(){
	    return "jzweb/trackDrawGuangDong/trackDrawGuangDong";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public Result<List<TrackDrawDO>> list(TrackDrawDO trackDrawDTO){

		return trackDrawService.selectDrawGuangDong(trackDrawDTO);
	}


	/**
	 * 导出抽签结果信息
	 *
	 * @param response
	 */
	@Log("导出抽签结果信息")
	@RequestMapping("/downloadImportTemplate")
	@ResponseBody
	public void downloadImportTemplate(HttpServletRequest request, HttpServletResponse response, TrackDrawDO trackDrawDTO) {
		Wrapper<TrackDrawDO> wrapper = new EntityWrapper<>();
		wrapper.orderBy(true,"sort");
		List<TrackDrawDO> trackDrawDOS = trackDrawService.selectList(wrapper);
		if (trackDrawDOS != null && !trackDrawDOS.isEmpty()) {
			for (TrackDrawDO trackDrawDO : trackDrawDOS) {
				if(Integer.parseInt(trackDrawDO.getTrack())<22){
					trackDrawDO.setTrack("第一天上午");
				}else{
					trackDrawDO.setTrack("第一天下午");
				}
				if(Integer.parseInt(trackDrawDO.getTrack2())<25){
					trackDrawDO.setTrack2("第二天上午");
				}else{
					trackDrawDO.setTrack2("第二天下午");
				}
			}
			//导出操作
			EasyPoiUtil.exportExcel(trackDrawDOS, "抽签信息导出", "抽签信息导出", TrackDrawDO.class,  "抽签信息导出", response);
		} else {
			throw new IFastApiException(ApiEnumError.NOTBATCHSTU.getCodeStr());
		}
	}

	/**
	 * 查询上次的抽签结果
	 */
	@ResponseBody
	@GetMapping("/trackList")
	public Result<TrackDrawDO> trackList(TrackDrawDO trackDrawDTO){

		return Result.ok(trackDrawService.trackList(trackDrawDTO));
	}


}
